clear all
clear all
#delimit ;
version 11.2;
set mem 650m;
set matsize 800;
set maxvar 32767;
set more off;
cap log close;


foreach debt in /*housing restbis*/ busloan/* credcard*/ {;

foreach y in 2002 /*2005*/ /*2008 2011 2014 2017*/ {;



cd "H:\Mi unidad\Grant_new\EFF";

use sample_eff.dta, clear;

cap drop facine3_imp1 facine3_imp2 facine3_imp3 facine3_imp4;
rename facine3_imp5 facine3;
svyset [pw=facine3];

foreach x in 1 2 3 4 5 { /* incorporate the rest of imputations */;

*global rhsage age1_imp`x' age2_imp`x' age3_imp`x'/* age4_imp`x' age5_imp`x' age6_imp`x' age7_imp`x' age8_imp`x'*/;
global rhsage age_imp`x' agesq_imp`x';
*global rhshh hh2_imp`x' hh3_imp`x' hh4_imp`x' hh5_imp`x' /*hh6_imp`x'*/;
*global rhshh hhincome_imp`x' hhincomesq_imp`x';
global rhshh lincome_imp`x';
*global rhsnw w2_imp`x' w3_imp`x' w4_imp`x' w5_imp`x'/**/;
global rhsnw w_imp`x';



drop if oth_inact_imp`x'==1 | disable_imp`x'==1 | student_imp`x'==1 | housew_imp`x'==1;
keep if age_imp`x'>=3 & age_imp`x' <=7.5;
drop if madult_imp`x'==1;

rename `debt'w1_imp`x' w1_imp`x';
rename `debt'w2_imp`x' w2_imp`x';
rename `debt'w3_imp`x' w3_imp`x';
rename `debt'w4_imp`x' w4_imp`x';
rename `debt'w5_imp`x' w5_imp`x';

cap rename l`debt'w_imp`x' w_imp`x';

cap drop age1_imp`x' age2_imp`x' age3_imp`x' age4_imp`x' age5_imp`x' age6_imp`x' age7_imp`x' age8_imp`x' age9_imp`x';

gen age1_imp`x'=0;
gen age2_imp`x'=0;
gen age3_imp`x'=0;
gen age4_imp`x'=0;

replace age1_imp`x'=1 if age_imp`x'>=3 & age_imp`x'<4.3;

replace age2_imp`x'=1 if age_imp`x'>=4.3 & age_imp`x'<5.5;

replace age3_imp`x'=1 if age_imp`x'>=5.5 & age_imp`x'<6.5;

replace age4_imp`x'=1 if age_imp`x'>=6.5;

replace hh5_imp`x'=1 if hh6_imp`x'==1;

gen risk_imp`x'= riskaver_imp`x'<=2;



/* cap noisily svy: probit l`debt'_imp`x' $rhsage univ_imp`x' bhealth_imp`x' sex_imp`x' couplewith_imp`x' couplewithout_imp`x' singlewith_imp`x'  risk_imp`x'
 $rhshh $rhsnw unemployed_imp`x' retiree_imp`x' selfemployed_imp`x' ration2_imp`x' if year==`y', ll(0)*/;
 
 
 
*Tobit for busloan; 
 cap noisily svy: tobit l`debt'_imp`x' $rhsage univ_imp`x' bhealth_imp`x' sex_imp`x' couplewith_imp`x' couplewithout_imp`x' singlewith_imp`x'  risk_imp`x'
 $rhshh $rhsnw unemployed_imp`x' retiree_imp`x' selfemployed_imp`x' ration2_imp`x' if year==`y', ll(0);

matrix b_imp`x'=e(b);
matrix v_imp`x'=e(V);

local i=1;
foreach var in age agesq /*age1 age2 age3*/ univ bhealth sex couplewith couplewithout singlewith  risk
 /*hh2 hh3 hh4 hh5*/ lincome /*w2 w3 w4 w5*/ w unemployed retiree selfemployed ration2 _cons {;

scalar mean_`var'_imp`x'=b_imp`x'[1,`i'];
scalar var_`var'_imp`x'=v_imp`x'[`i',`i'];

local ++i;
};
*scalar mean_cons_imp`x'=b_imp`x'[1,22];
*scalar var_cons_imp`x'=v_imp`x'[22,22];


} /* foreach x in ... */;

local h=1;
foreach var in age agesq /*age1 age2 age3*/ univ bhealth sex couplewith couplewithout singlewith  risk
 /*hh2 hh3 hh4 hh5*/ lincome /*w2 w3 w4 w5*/ w unemployed retiree selfemployed ration2 _cons {;

scalar summean_`var'0 = 0;
scalar sumvar_`var'0 = 0;

forvalues i = 1(1)5 {;
local j = `i' - 1;
scalar summean_`var'`i' = scalar(summean_`var'`j') + scalar(mean_`var'_imp`i');
scalar sumvar_`var'`i' = scalar(sumvar_`var'`j') + scalar(var_`var'_imp`i');
} ;

scalar mean_`var' = (1/5)*scalar(summean_`var'5);
scalar u_`var' = (1/5)*scalar(sumvar_`var'5);

local ++h;

};
******************************;
forvalues x = 1(1)5 {;
local i=1;
foreach var in age agesq /*age1 age2 age3*/ univ bhealth sex couplewith couplewithout singlewith  risk
 /*hh2 hh3 hh4 hh5*/ lincome /*w2 w3 w4 w5*/ w unemployed retiree selfemployed ration2 _cons{;

scalar difmeansq_`var'_imp`x'=(b_imp`x'[1,`i']-scalar(mean_`var'))^2;

local ++i;
};
};

**************;
foreach var in age agesq /*age1 age2 age3*/ univ bhealth sex couplewith couplewithout singlewith  risk
 /*hh2 hh3 hh4 hh5*/ lincome /*w2 w3 w4 w5*/ w unemployed retiree selfemployed ration2 _cons {;

scalar sumdifmeansq_`var'0 = 0;

forvalues i = 1(1)5 {;
local j = `i' - 1;
scalar sumdifmeansq_`var'`i' = sumdifmeansq_`var'`j' + difmeansq_`var'_imp`i';
} ;

scalar b_`var'=(1/4)*sumdifmeansq_`var'5;
scalar t_`var'=scalar(u_`var')+(6/5)*scalar(b_`var');
scalar sqt_`var'=scalar(t_`var')^(1/2);
scalar chi2_`var'=(scalar(mean_`var')^2)/(scalar(sqt_`var')^2);
scalar pchi2_`var'=chi2tail(1,scalar(chi2_`var'));
};

log using "H:\Mi unidad\Grant_new\SERIEs\2nd_revision\tobit_year`y'_`debt'.smcl", replace;

di "Year " `y' ; 
di "`debt'";

foreach var in age agesq /*age1 age2 age3*/ univ bhealth sex couplewith couplewithout singlewith  risk
 /*hh2 hh3 hh4 hh5*/ lincome /*w2 w3 w4 w5*/ w unemployed retiree selfemployed ration2 _cons{;
		
di "`var'";	
di %4.3f scalar(mean_`var');
di %4.3f scalar(sqt_`var');
di %4.3f scalar(pchi2_`var');

};	

cap log close;
} /* foreach y in year */;
}/* foreach var in debts */;
